﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            new Fight().Begin();
        }

        class Fight
        {

            public Fight() { }
        
            public void Begin()
            {
                Console.WriteLine("*****************************");
                Console.WriteLine("*********猜拳游戏************");
                Console.WriteLine("*****************************");
                Console.WriteLine("出拳规则，石头剪刀布");
                Console.WriteLine("请输入玩家姓名:");
                string str = Console.ReadLine();
                RandomGamer(str);
            }

            public void RandomGamer(string gamer)
            {

                string[] name = new string[] { "刘备", "关羽", "张飞", "刘禅", "秦始皇", "唐玄宗", "唐三藏", "孙悟空", "猪八戒", "沙僧", "李小龙", "周星驰", "叶辰", "王昊", "王维", "李白", "张三", "司马迁", "司马懿", "荀子", "老子", "阿基米德", "爱因斯坦", "牛顿", "夏洛特" };
                string[] arr = new string[3];
                for (int i = 0; i < 3; i++)
                {
                    Random ran = new Random();
                    int num = ran.Next(0, 25);
                    if (!arr.Contains(name[num]))
                    {
                        arr[i] = name[num];
                    }
                    else
                    {
                        i--;
                    }

                }
                Console.WriteLine($"玩家:{gamer},角色1:{arr[0]},角色2:{arr[1]},角色3:{arr[2]}");
            a: Console.WriteLine("是否开始游戏[Y/N]");
                string str = Console.ReadLine();
                switch (str)
                {
                    case "Y":
                        RandomFight(arr, gamer);
                        break;
                    case "N":
                        Begin();
                        break;
                    default:
                        Console.WriteLine("输入错误");
                        goto a;
                }
            }

            public void RandomFight(string[] newName, string gamer)
            {

                int count = 1;
                int[] arr = new int[3];
                while (true)
                {
                    Console.WriteLine($"Round {count}");

                    int num = FightWhat();

                    if (num == 1)
                    {
                        bool flag = GuessOne(newName, gamer, 1, count);
                        

                    }
                    else if (num == 2)
                    {
                        GuessOne(newName, gamer, 2, count);
                    }
                    else
                    {
                        GuessOne(newName, gamer, 3, count);

                    }
                }

            }

            private bool GuessOne(string[] newName, string gamer, int g1,int count)
            {
                int g2 = 0; 
                int g3 = 0;
                
                if (g1 == 1)
                {
                    g2 = 2;
                    g3 = 3;
                }
                else if (g1 == 2) {
                    g2 = 3;
                    g3 = 1;
                }
                else {
                    g2 = 1;
                    g3 = 2;
                }
                int[] arr = new int[3];
                Random ran = new Random();
                for (int i = 0; i < arr.Length; i++)
                {
                    int num = ran.Next(1,4);
                    arr[i] = num;
                }
                arr[0] = g1; arr[1] = g1; arr[2] = g3;
                
                if ((arr[0] == g2 && arr[1] == g1 && arr[2] == g1) || (arr[0] == g1 && arr[1] == g1 && arr[2] == g2) || (arr[0] == g1 && arr[1] == g2 && arr[2] == g1))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == g2)
                        {
                            string punch = GuessPunch(g2);
                            Console.WriteLine($"出牌者 {newName[i]}赢了，出了{punch}，{gamer} 为最后一名");
                            return true;
                        }
                    }

                }
                else if (arr[0] == g3 && arr[1] == g3 && arr[2] == g3)
                {
                    Console.WriteLine($"玩家 {gamer}赢了，通吃！");
                    return true;
                }
                else if ((arr[0] == g1 && arr[1] == g3 && arr[2] == g3) || (arr[0] == g3 && arr[1] == g1 && arr[2] == g3) || (arr[0] == g3 && arr[1] == g3 && arr[2] == g1))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == g1)
                        {
                            Console.WriteLine($"出{GuessPunch(g1)}者胜！！！");
                            Console.WriteLine($"玩家 {gamer}和出牌者 {newName[i]}赢了,进入下一轮厮杀");
                            string[] name = new string[] { newName[i] };
                            NextFight(count, gamer, name);

                        }
                    }
                }
                else if ((arr[0] == g1 && arr[1] == g3 && arr[2] == g1) || (arr[0] == g1 && arr[1] == g1 && arr[2] == g3) || (arr[0] == g3 && arr[1] == g1 && arr[2] == g1))
                {
                    string[] name = new string[2];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == g1)
                        {
                            int count2 = 1;

                            if (count2 == 1) { name[0] = newName[i]; }
                            else
                            {
                                name[1] = newName[i];
                            }
                            count++;

                        }
                    }
                    Console.WriteLine($"出{GuessPunch(g1)}者胜！！！不对劲，人数有点多！");
                    Console.WriteLine($"玩家 {gamer}和出牌者 {name[0]},{name[1]}赢了,进入下一轮厮杀");
                    NextFight(count, gamer, name);
                }
                else
                {
                    Console.WriteLine($"谁都没落着好，下一把");
                    return false;
                }
                return false;
            }

            private string GuessPunch(int num)
            {
                switch (num) {
                    case 1:
                        return "剪刀";
                    case 2:
                        return "拳头";
                    case 3:
                        return "布";
                    default:
                        return "不存在的掌法";
                }
                
            }

            public int FightWhat()
            {

                Console.WriteLine("请输入你要出：1、剪刀,2、石头,3、布");
                int num;
            b: try
                {
                    num = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("输入错误");
                    goto b;
                }

                return num;
            }

            private void NextFight(int count, string gamer, string[] name)
            {
                if (name.Length == 1)
                {
                c: int num = FightWhat();
                    Console.WriteLine($"Round {count}");

                    int ran = new Random().Next(1, 4);
                    if (num == 1)
                    {
                        if (!GuessTwo(1, name, gamer,ran)) { count++; goto c; } else { FinalThing(count, gamer); }

                    }
                    else if (num == 2)
                    {
                        if (!GuessTwo(2, name, gamer,ran)) { count++; goto c; } else { FinalThing(count, gamer); }
                    }
                    else
                    {
                        if (!GuessTwo(3, name, gamer,ran)) { count++; goto c; } else { FinalThing(count, gamer); }
                    }


                }
                else
                {
                   
                d: int num = FightWhat();
                    Console.WriteLine($"Round {count}");
                    

                    if (num == 1)
                    {
                        if (!GuessThree(1, gamer, name, count)) { count++; goto d; } else { FinalThing(count,gamer); }
                    }
                    else if (num == 2)
                    {

                        if (!GuessThree(2, gamer, name, count)) { count++; goto d; } else { FinalThing(count, gamer); }

                    }
                    else
                    {
                        if (!GuessThree(3, gamer, name, count)) { count++; goto d; } else { FinalThing(count, gamer); }
                    }

                }
            }

            private void FinalThing(int count,string gamer)
            {
                int count2 = 1;
                count2++;
                f: Console.WriteLine($"玩家 【{gamer}】 ,您总共进行了 {count} 轮，是否继续?[Y/N]");
                string str = Console.ReadLine();
                switch (str) {
                    case "Y":
                        Console.WriteLine($"游戏继续,当前为第 {count2} 大轮");
                        RandomGamer(gamer);
                        break;
                    case "N":
                        Process.GetCurrentProcess().Kill();
                        break;
                    default:
                        Console.WriteLine("输入错误");
                        goto f;
                }
            }

           

            private bool GuessTwo(int num,string[] name,string gamer,int ran)
            {
                int num2 = 0;
                int num3 = 0;
                if (num == 1)
                {
                    num2 = 2;
                    num3 = 3;
                }
                else if (num == 2)
                {
                    num2 = 3;
                    num3 = 1;
                }
                else {
                    num2 = 1;
                    num3 = 2;
                }
                if (ran == num2)
                {
                    Console.WriteLine($"出{GuessPunch(ran)}者胜！！！");
                    Console.WriteLine($"出牌者 {name[0]} 赢了!");
                    return true;
                }
                else if (ran == num3)
                {
                    Console.WriteLine($"出{GuessPunch(num)}者胜利了！！！Victory!!!");
                    Console.WriteLine($"玩家 {gamer} 获得了最终的胜利！");
                    return true;
                }
                else
                {
                    Console.WriteLine("再来一把！快要分胜负了！");
                    return false;
                }

            }

            private bool GuessThree(int num, string gamer, string[] name, int count)
            {
                int num2 = 0;
                int num3 = 0;
                int[] arr = new int[2];
                Random ran = new Random();
                for (int i = 0; i < arr.Length; i++)
                {
                    int r = ran.Next(1, 4);
                    arr[i] = r;
                }
                if (num == 1)
                {
                    num2 = 3;
                    num3 = 2;
                }
                else if (num == 2)
                {
                    num2 = 1;
                    num3 = 3;
                }
                else
                {
                    num2 = 2;
                    num3 = 1;
                }
                if ((arr[0] == num && arr[1] == num2) || (arr[0] == num2 && arr[1] == num))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == num)
                        {
                            Console.WriteLine($"出{GuessPunch(num)}了，比赛结果是什么？！");
                            Console.WriteLine($"玩家 {gamer}和出牌者 {name[i]}进入总决赛！！");
                            string[] name2 = new string[] { name[i] };
                            count++;
                            NextFight(count, gamer, name2);
                        }
                    }

                }
                else if ((arr[0] == num && arr[1] == num3) || (arr[0] == num3 && arr[1] == num))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == num3)
                        {
                            Console.WriteLine($"对手狡猾的出了{GuessPunch(num3)}");
                            Console.WriteLine($"出牌者{name[i]}获得了最终胜利");
                            return true;
                        }
                    }
                }
                else if (arr[0] == num2 && arr[1] == num2)
                {
                    Console.WriteLine($"玩家使出了{GuessPunch(num)}");
                    Console.WriteLine($"玩家{gamer}赢得最终胜利");
                    return true;
                }
                else
                {
                    Console.WriteLine("再来一轮");
                    Console.WriteLine("双方的运气都很不错，结局将会鹿死几手？");
                    if (count % 3 == 0) Console.WriteLine("幸运彩蛋");
                    return false;
                }
                return false;
            }
        }
    }
}
